package com.example.demo.controller;

import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import com.example.demo.entity.session.Book;
import com.example.demo.entity.session.DB;
import com.example.demo.entity.User;
import com.example.demo.entity.UserVo;
import com.example.demo.global.response.ResponseCode;
import com.example.demo.global.response.ResponseVo;
import com.example.demo.global.test.ConstValue;

import io.swagger.annotations.ApiOperation;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.websocket.server.PathParam;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;
import java.util.Set;

@RestController
@RequestMapping("/user")
@CrossOrigin
public class UserLogin {

    @ApiOperation(value = "用户登录")
    @PostMapping(value = "/login")
    /**
     * 这个方法是Form表单的形式，可以传输文件信息
     */
    public void login(User user, MultipartFile file){// 前端和后台传输Form 表单，会进行自动适配，字段是否匹配均不要紧，他只匹配自己想要的东西，前提 路由匹配
        System.out.println(user.toString());
        if(file != null){
            System.out.println(file.getOriginalFilename() + file.getSize());
        }
    }

    @ApiOperation(value = "用户登录")
    @PostMapping(value = "/login/json")
    /**
     * 这个方法是Json 表单的形式，传输json数据
     */
    public void loginJson(@RequestBody User user){ //前端和后台传输Json 字段，会进行自动适配，字段是否匹配均不要紧，他只匹配自己想要的东西，前提 路由匹配
        System.out.println(user.toString());
    }

    @GetMapping(value = "/{id}/info")
    @ResponseBody
    public User getUserInfo(@PathParam(value = "id") Integer userid){
        User user = new User();
        user.setUsername("hello");
        return user;
    }
    @ApiOperation(value = "用户识别")
    @PostMapping(value = "/singIn/file")
    public ResponseVo userVerify(User user,MultipartFile file){ // user中不能有File对象存在，file必须在外面
        UserVo userVo = new UserVo();
        userVo.setName("王先生");
        if(file != null){
            userVo.setImgData(ConstValue.imgData);
            return new ResponseVo(ResponseCode.SUCCESS,userVo);
        }
        return new ResponseVo(ResponseCode.EMPTYRESULT,null);
    }

    @ApiOperation(value = "用户识别")
    @GetMapping(value = "/singIn/data")
    public ResponseVo userVerify(@RequestParam("imgData") String imgData){
        UserVo userVo = new UserVo();
        userVo.setName("王先生");
        if(imgData != null){
            userVo.setImgData(ConstValue.imgData);
            return new ResponseVo(ResponseCode.SUCCESS,userVo);
        }
        return new ResponseVo(ResponseCode.EMPTYRESULT,null);
    }

    @ApiOperation(value = "用户登录")
    @GetMapping(value = "/request")
    public void getUserLogin(HttpServletRequest request, HttpServletResponse response){



//        String uuid = UUID.randomUUID().toString();
//        CookieUtil.set(response,"TOKEN",uuid,3600);

//        HttpSession session = request.getSession();
//        System.out.println(session.getId());
//        session.setAttribute("name","zhangsan");


        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        //使用request对象的getSession()获取session，如果session不存在则创建一个
        HttpSession session = request.getSession();
        PrintWriter out = null;
        try {
            out = response.getWriter();
        } catch (IOException e) {
            e.printStackTrace();
        }

        out.write("本网站有如下书：<br/>");
        Set<Map.Entry<String,Book>> set = DB.getAll().entrySet();
        for(Map.Entry<String,Book> me : set){
            Book book = me.getValue();
            String url =request.getContextPath()+ "/user/request?id=" + book.getId();
            //response. encodeURL(java.lang.String url)用于对表单action和超链接的url地址进行重写
            url = response.encodeURL(url);//将超链接的url地址进行重写
            out.println(book.getName()  + "   <a href='"+url+"'>购买</a><br/>");
        }

        //将数据存储到session中
//        session.setAttribute("data", "孤傲苍狼");
//        //获取session的Id
//        String sessionId = session.getId();
//        //判断session是不是新创建的
//        if (session.isNew()) {
//            try {
//                response.getWriter().print("session创建成功，session的id是："+sessionId);
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//        }else {
//            try {
//                response.getWriter().print("服务器已经存在该session了，session的id是："+sessionId);
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//        }

//        return "OK";
    }
}
